package com.xh.jf_blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.jf_blog.bean.Article;
import com.xh.jf_blog.bean.ArticleTag;
import com.xh.jf_blog.bean.Tag;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.*;
import com.xh.jf_blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/2/21/021
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleLikeMapper articleLikeMapper;
    @Autowired
    private CommentMapper commentMapper;

    public List<Article> getListByAnthologyId(int anthologyId) {
        Article article = new Article();
        article.setAnthologyId(anthologyId);
        return articleMapper.getAll(article);
    }

    public Article getById(int articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    public void saveOrUpdate(Article article) {
        if (article.getArticleStatus() != null && article.getArticleStatus() == 1) {
            article.setApplyTime(new Date());
        }
        if (article.getArticleId() == null) {
            articleMapper.insertSelective(article);
        } else {
            articleMapper.updateByPrimaryKeySelective(article);
        }
        List<Tag> tagList = new ArrayList<Tag>();
        /*批量处理新增的tag*/
        if (article.getNewTags() != null && article.getNewTags().length > 0) {
            for (String name : article.getNewTags()) {
                Tag tag = new Tag();
                tag.setUserinfoId(article.getUserinfoId());
                tag.setName(name);
                tag.setCreateTime(new Date());
                tag.setCreateUser(article.getUserinfoId());
                tagList.add(tag);
            }
            tagMapper.batchInsert(tagList);
        }


        /*删除关联的tag数据*/
        articleTagMapper.deleteByArticleId(article.getArticleId());
        List<ArticleTag> articleTagList = new ArrayList<ArticleTag>();
        /*封装批量添加的集合*/
        if (article.getTagIds() != null) {
            for (Integer tagId : article.getTagIds()) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getArticleId());
                articleTag.setTagId(tagId);
                articleTagList.add(articleTag);
            }
        }
        if (!tagList.isEmpty()) {
            for (Tag tag : tagList) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getArticleId());
                articleTag.setTagId(tag.getTagId());
                articleTagList.add(articleTag);
            }
        }
        if (!articleTagList.isEmpty()) {
            articleTagMapper.batchInsert(articleTagList);
        }

    }

    public void deleteById(int articleId) {
        articleTagMapper.deleteByArticleId(articleId);
        articleMapper.deleteByPrimaryKey(articleId);
        commentMapper.deleteByArticleId(articleId);
        articleLikeMapper.deleteCollectionByArticleId(articleId);
        articleLikeMapper.deleteLikeByArticleId(articleId);
    }

    public List<Article> getHotArticle() {
        return articleMapper.getHotList();
    }

    public List<Article> getList(Article article) {
        return articleMapper.getAll(article);
    }

    public R getPage(int page, int limit, Article article) {
        PageHelper.startPage(page, limit);
        List<Article> list = articleMapper.getAll(article);
        PageInfo<Article> pageInfo = new PageInfo<Article>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void updateReadCount(int articleId) {
        articleMapper.updateReadCount(articleId);
    }
}
